package com.huqing.icu.base.redis.proxy;

import com.huqing.icu.SpringBootApplicationContextUtils;
import com.huqing.icu.base.redis.config.RedisClientConfig;
import com.huqing.icu.base.redis.config.RedisDeployType;
import com.huqing.icu.base.redis.operate.ClusterRedisOperation;
import com.huqing.icu.base.redis.operate.RedisOperation;
import com.huqing.icu.base.redis.operate.SingleRedisOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Description Redis操作代理
 * @Author huqing
 * @Date 2025/1/6 10:50
 **/
@Component
public class RedisOperationProxy {

    @Autowired
    private RedisClientConfig redisClientConfig;

    /**
     * 这个方法的作用：如果配置的是单机，则返回单机操作类，把Redis操作以单机的方式去执行，
     * 如果配置的是集群，则返回集群操作类，把Redis操作以集群的方式去执行
     *
     * @return
     */
    public RedisOperation getOperation() {
        RedisOperation redisOperation = null;
        String used = redisClientConfig.getUsed();
        if (RedisDeployType.CLUSTER.getCode().equals(used))
            redisOperation = SpringBootApplicationContextUtils.getBean(ClusterRedisOperation.class);
        else if (used == RedisDeployType.SINGLE.getCode())
            redisOperation = SpringBootApplicationContextUtils.getBean(SingleRedisOperation.class);
        return redisOperation;
    }
}
